
package cn.edu.scau.cmi.hombio.dao;

import cn.edu.scau.cmi.hombio.domain.PigHouse;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage PigHouse entities.
 * 
 */
@Repository("PigHouseDAO")

@Transactional
public class PigHouseDAOImpl extends AbstractJpaDao<PigHouse> implements PigHouseDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] {
			PigHouse.class }));

	/**
	 * EntityManager injected by Spring for persistence unit pig
	 *
	 */
	@PersistenceContext(unitName = "pig")
	private EntityManager entityManager;

	/**
	 * Instantiates a new PigHouseDAOImpl
	 *
	 */
	public PigHouseDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findPigHouseByMemo
	 *
	 */
	@Transactional
	public Set<PigHouse> findPigHouseByMemo(String memo) throws DataAccessException {

		return findPigHouseByMemo(memo, -1, -1);
	}

	/**
	 * JPQL Query - findPigHouseByMemo
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PigHouse> findPigHouseByMemo(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigHouseByMemo", startResult, maxRows, memo);
		return new LinkedHashSet<PigHouse>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigHouseByRfid
	 *
	 */
	@Transactional
	public Set<PigHouse> findPigHouseByRfid(String rfid) throws DataAccessException {

		return findPigHouseByRfid(rfid, -1, -1);
	}

	/**
	 * JPQL Query - findPigHouseByRfid
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PigHouse> findPigHouseByRfid(String rfid, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigHouseByRfid", startResult, maxRows, rfid);
		return new LinkedHashSet<PigHouse>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigHouseByNumber
	 *
	 */
	@Transactional
	public Set<PigHouse> findPigHouseByNumber(Integer number) throws DataAccessException {

		return findPigHouseByNumber(number, -1, -1);
	}

	/**
	 * JPQL Query - findPigHouseByNumber
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PigHouse> findPigHouseByNumber(Integer number, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigHouseByNumber", startResult, maxRows, number);
		return new LinkedHashSet<PigHouse>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigHouseByRfidContaining
	 *
	 */
	@Transactional
	public Set<PigHouse> findPigHouseByRfidContaining(String rfid) throws DataAccessException {

		return findPigHouseByRfidContaining(rfid, -1, -1);
	}

	/**
	 * JPQL Query - findPigHouseByRfidContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PigHouse> findPigHouseByRfidContaining(String rfid, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigHouseByRfidContaining", startResult, maxRows, rfid);
		return new LinkedHashSet<PigHouse>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllPigHouses
	 *
	 */
	@Transactional
	public Set<PigHouse> findAllPigHouses() throws DataAccessException {

		return findAllPigHouses(-1, -1);
	}

	/**
	 * JPQL Query - findAllPigHouses
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PigHouse> findAllPigHouses(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllPigHouses", startResult, maxRows);
		return new LinkedHashSet<PigHouse>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigHouseByMemoContaining
	 *
	 */
	@Transactional
	public Set<PigHouse> findPigHouseByMemoContaining(String memo) throws DataAccessException {

		return findPigHouseByMemoContaining(memo, -1, -1);
	}

	/**
	 * JPQL Query - findPigHouseByMemoContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<PigHouse> findPigHouseByMemoContaining(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigHouseByMemoContaining", startResult, maxRows, memo);
		return new LinkedHashSet<PigHouse>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigHouseByPrimaryKey
	 *
	 */
	@Transactional
	public PigHouse findPigHouseByPrimaryKey(Integer id) throws DataAccessException {

		return findPigHouseByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findPigHouseByPrimaryKey
	 *
	 */

	@Transactional
	public PigHouse findPigHouseByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPigHouseByPrimaryKey", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.PigHouse) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findPigHouseById
	 *
	 */
	@Transactional
	public PigHouse findPigHouseById(Integer id) throws DataAccessException {

		return findPigHouseById(id, -1, -1);
	}

	/**
	 * JPQL Query - findPigHouseById
	 *
	 */

	@Transactional
	public PigHouse findPigHouseById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPigHouseById", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.PigHouse) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(PigHouse entity) {
		return true;
	}
}
